#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use cp.x to perform"
$ECHO "Born-Oppenheimer molecular dynamics using the"
$ECHO "conjugate gradient minimization of the electronic"
$ECHO "states. It shows also the use of ensemble-DFT"
$ECHO "for metallic systems."
$ECHO "The example shows a Si dimer"

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="cp.x"
PSEUDO_LIST="Si.pbe-rrkj.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE \
                http://www.quantum-espresso.org/pseudo/1.3/UPF/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
CP_COMMAND="$PARA_PREFIX $BIN_DIR/cp.x $PARA_POSTFIX"
$ECHO
$ECHO "  running cp.x as: $CP_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

# molecular dynamics calculation
cat > si2.ensemble-dyn.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="cp" prefix="Si_dimer">

	<cell type="qecell">
		<qecell ibrav="8" alat="10.0">
			<real rank="1" n1="5">
				1.0 1.5 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Si">
			<property name="mass">
				<real>28.086</real>
			</property>
			<property name="pseudofile">
				<string>Si.pbe-rrkj.UPF</string>
			</property>
			<property name="ion_radius">
				<real>0.8d0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="bohr" nat="2" >
		<atom name="Si">
			<position ifx="1" ify="1" ifz="1">
				<real rank="1" n1="3">
					0.0	0.0	0.0  
				</real>
			</position>
		</atom>	
		<atom name="Si">
			<position ifx="1" ify="1" ifz="1">
				<real rank="1" n1="3">
					0.0 0.0 5.5
				</real>
			</position>
		</atom>			
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>
		
		<parameter name="iprint">
			<integer>
				10
			</integer>
		</parameter>
		
		<parameter name="tstress">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="tprnfor">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="startingwfc">
			<string>
				random
			</string>
		</parameter>
		
		<parameter name="ion_positions">
			<string>
				from_input
			</string>
		</parameter>
	
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				15.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				60.0
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.d-6
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				8
			</integer>
		</parameter>
		
		<parameter name="occupations">
			<string>
				ensemble
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				fd
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.025
			</real>
		</parameter>

	</field>
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				1
			</integer>
		</parameter>
		
	</field>
	
	<field name="CP">
	
<!--nstep, dt, ion_dynamics e ion_temperature esistono anche nel PW 
ma nn sapevo in che field metterli  -->
		<parameter name="nstep">
			<integer>
				5
			</integer>
		</parameter>
		
		<parameter name="dt">
			<real>
				10.0d0
			</real>
		</parameter>
		
		<parameter name="ion_dynamics">
			<string>
				damp
			</string>
		</parameter>
		
		<parameter name="ion_temperature">
			<string>
				not_controlled
			</string>
		</parameter>
		
<!-- Da qui in poi esistono solo in CP-->
		
		<parameter name="isave">
			<integer>
				10
			</integer>
		</parameter>
		
		<parameter name="nr1b">
			<integer>
				20
			</integer>
		</parameter>
		
		<parameter name="nr2b">
			<integer>
				20
			</integer>
		</parameter>
		
		<parameter name="nr3b">
			<integer>
				20
			</integer>
		</parameter>
		
		<parameter name="emass">
			<real>
				1000.d0
			</real>
		</parameter>
		
		<parameter name="emass_cutoff">
			<real>
				4.d0
			</real>
		</parameter>
		
		<parameter name="orthogonalization">
			<string>
				Gram-Schmidt
			</string>
		</parameter>
		
		<parameter name="ampre">
			<real>
				0.02
			</real>
		</parameter>
		
		<parameter name="n_inner">
			<integer>
				8
			</integer>
		</parameter>
		
		<parameter name="tcg">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="passop">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="maxiter">
			<integer>
				250
			</integer>
		</parameter>
		
<!-- non esiste nel CP Help ion_damping -->		
		<parameter name="ion_damping">
			<real>
				0.0
			</real>
		</parameter>
<!--  -->

		<parameter name="greasp">
			<real>
				1.0
			</real>
		</parameter>
			
	</field>
	
</input>
EOF
$ECHO "  running the calculation BO-MD for Si-dimer...\c"
$CP_COMMAND < si2.ensemble-dyn.xml > si2.ensemble-dyn.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR : done"
